package com.yzh.luckydraw.domain.supports.ids.policy;

import com.yzh.luckydraw.domain.supports.ids.IIdGenerator;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Random;

/**
 * 短码生成策略，仅支持很小的调用量，用于生成活动配置类编号，保证全局唯一
 */
@Component
public class ShortCode implements IIdGenerator {
    @Override
    public long nextId() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int second = calendar.get(Calendar.SECOND);
        //年月日+每秒+随机四位数
        StringBuilder idStr = new StringBuilder();
        idStr.append(year);
        idStr.append(month);
        idStr.append(day);
        idStr.append(second);
        idStr.append(String.format("%04d", new Random().nextInt(10000)));
        System.out.println(month);
        return Long.parseLong(idStr.toString());
    }
}
